/*
 * Copyright (c) 2007
 *	Alberto Magni, Maggioni Marcello
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

package servlets;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.activation.*;
import javax.servlet.*;
import javax.servlet.http.*;
import exceptions.*;
import beans.*;
import emailsender.*;
import javax.mail.SendFailedException;
/**@author Alberto Magni
 * This class set all the parameters for the email an call the method that actually
 * sends the email.*/
public class SenderSrv extends HttpServlet 
{
    /**Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response*/
    @Override
    protected void doGet (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 
    {
        EMailSender sender=new EMailSender();
        //set up the parameters
        MailBean mb = (MailBean) request.getSession().getAttribute("maildatabase");
        sender.setRecipients(mb.getRecipients());
        sender.setMessage(mb.getMessage());
        sender.setTransport(mb.getTransport());
        try
        {
            //send the email
            sender.sendMail();
        }
        catch(SendFailedException e)
        {
            //notify the failure
            String text= new String(e.toString());
            SendFailedException sfe=null;
            if(text.indexOf("STARTTLS")!=-1)
            {
                sfe=new SendFailedException("Check if your server needs authentication");
            }
            else
            {
                sfe=new SendFailedException("An address could be malformed !");
                
            }
            request.setAttribute("exception",sfe);
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
        catch(Exception exception)
        {
            //notify the failure
            request.setAttribute("exception",exception);
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
        //notify the success
        request.getSession().invalidate();
        response.sendRedirect("success.jsp");
    }
}
